﻿using System;
using System.Collections.ObjectModel;
using System.Web.UI.WebControls;
using NPMonitor.DataModel;
using NPMonitor.Implementation;

namespace NPMonitor.Web.UserControls
{
    public partial class MachineList : System.Web.UI.UserControl
    {
        /// <summary>
        /// 每行索引
        /// </summary>
        protected int itemIndex = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            ctPager.PageChange += new PagerControl.PageRefresh(BindData);
            PoolId = Request.Params["poolId"];
            if (!base.IsPostBack)
            {
                BindData(0);
            }
            phSaveSuccessful.Visible = false;
        }

        #region 属性
        /// <summary>
        /// 页面大小
        /// </summary>
        public int PageSize
        {
            get { return ctPager.PageSize; }
        }

        /// <summary>
        /// 当前页数
        /// </summary>
        public int CurrentPageIndex
        {
            get { return ctPager.CurrentPageIndex; }
        }

        /// <summary>
        /// 机器所属的池id
        /// </summary>
        public string PoolId
        {
            get
            {
                return ViewState["MachinePoolId"] as string;
            }
            set
            {
                ViewState["MachinePoolId"] = value;
            }
        }

        /// <summary>
        /// 机器记录总数
        /// </summary>
        public int ItemsCount
        {
            get
            {
                return Convert.ToInt32(ViewState["MachineItemsCount"]);
            }

            set
            {
                ViewState["MachineItemsCount"] = value;
            }
        }

        /// <summary>
        /// 获取PagerControl
        /// </summary>
        public PagerControl ListPagerControl { get { return this.ctPager; } }

        #endregion

        #region 方法

        /// <summary>
        /// 绑定机器列表
        /// </summary>
        public void BindData(int pageIndex)
        {
            int machineTotalCount = 0;
            ReadOnlyCollection<MachineData> list = null;
            //如果pageInfex等于-1，则是当前页面
            if (pageIndex == -1)
            {
                pageIndex = ctPager.CurrentPageIndex;
            }

            //所有机器
            if (string.IsNullOrWhiteSpace(PoolId))
            {
                machineTotalCount = StoreManager.Current.MachineStore.GetAllMachinesTotalCount();
                ctPager.IntialProperties(machineTotalCount);
                pageIndex = ctPager.CurrentPageIndex;
                list = StoreManager.Current.MachineStore.GetAllMachines(pageIndex, PageSize);
            }
            else
            {
                //根据pool id 获取机器
                int poolId = Convert.ToInt32(PoolId);
                machineTotalCount = StoreManager.Current.MachineStore.GetAllMachinesTotalCountByPoolId(poolId);
                ctPager.IntialProperties(machineTotalCount);
                pageIndex = ctPager.CurrentPageIndex;
                list = StoreManager.Current.MachineStore.GetMachinesByPoolId(poolId, pageIndex, PageSize);
            }

            this.rptMachineList.DataSource = list;
            this.rptMachineList.DataBind();

            ItemsCount = machineTotalCount;
        }

        /// <summary>
        /// 根据pool id 获取pool名字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptMachineList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Literal litPoolId = e.Item.FindControl("litPoolId") as Literal;
            int poolId = Convert.ToInt32(litPoolId.Text);
            MachinePool poolInfo = StoreManager.Current.MachineStore.GetMachinePoolInfoById(poolId);
            if (poolInfo != null)
            {
                litPoolId.Text = poolInfo.PoolName;
            }
            else
            {
                litPoolId.Text = "";
            }
        }

        /// <summary>
        /// 显示机器编辑信息
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptMachineList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                if (e.CommandName == "Edit")
                {
                    string id = e.CommandArgument.ToString();
                    ctMachineInfo.MachineId = int.Parse(id);
                    ctMachineInfo.ShowMachineInfo(ctMachineInfo.MachineId);
                    ctMachineInfo.PhPopEdit = true;
                }
            }
        }

        /// <summary>
        /// 刷新页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgRefresh_OnClick(object sender, EventArgs e)
        {
            this.BindData(ctPager.CurrentPageIndex);
        }

        public void ShowSuccessfulDiv()
        {
            phSaveSuccessful.Visible = true;
        }

        #endregion
    }
}